package de.windeln.marketing.campaign.costinterval;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import de.windeln.marketing.campaign.costinterval.dailyCost.services.interfaces.DailyCostService;
import de.windeln.marketing.campaign.costinterval.model.CostintervalClientObject;
import de.windeln.marketing.campaign.costinterval.services.interfaces.CostIntervalService;
import de.windeln.marketing.campaign.services.interfaces.CampaignService;
import de.windeln.marketing.utils.RandomNumberGenerator;
import de.windeln.marketing.validation.CostIntervalInBetweenCampaignInterval;
import de.windeln.marketing.validation.CostIntervalStartAfterEnd;
import de.windeln.marketing.validation.FixCostsOrNumberOfVariableMustBeGiven;
import de.windeln.marketing.validation.OverlappingCostIntervals;

@Controller
public class CostIntervalController {

	@Resource(name = "costIntervalService")
	private CostIntervalService costIntervalService;
	@Resource(name = "dailyCostService")
	private DailyCostService dailyCostService;
	@Resource(name = "campaignService")
	private CampaignService campaignService;
	@Autowired
	private CostIntervalValueGenerator costIntervalValueGenerator;
	@Resource(name = "costIntervalStartAfterEnd")
	private CostIntervalStartAfterEnd costIntervalStartAfterEnd;
	@Resource(name = "fixCostsOrNumberOfVariableMustBeGiven")
	private FixCostsOrNumberOfVariableMustBeGiven fixCostsOrNumberOfVariableMustBeGiven;
	@Resource(name = "costIntervalInBetweenCampaignInterval")
	private CostIntervalInBetweenCampaignInterval costIntervalInBetweenCampaignInterval;
	@Resource(name = "overlappingCostIntervals")
	private OverlappingCostIntervals overlappingCostIntervals;
	
	@RequestMapping(value = "/costIntervals*", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody Map fetchAllCostIntervalForCampaign(@RequestParam int ID) {
		Map<String, Object> container = new HashMap<String, Object>();
		container.put("aaData", costIntervalService.fetchAllCostIntervalForCampaign(ID));
		return container;
	}
	
	@RequestMapping(value = "/costIntervals*", method = RequestMethod.POST, produces = "application/json")
	public @ResponseBody
	Map createNewCostInterval(@Valid @RequestBody CostintervalClientObject costInterval) {	
		costIntervalValueGenerator.generate(costInterval);
		costInterval.setId(RandomNumberGenerator.generateRandomUniqueIDAsLong());
		costIntervalService.insertNewCostInterval(costInterval);
		dailyCostService.insertNewDailyCosts(costInterval);
		campaignService.updateCampaignAfterInsertingCostInterval(costInterval);
		return null;
	}
	
	
	@RequestMapping(value = "/costIntervals*", method = RequestMethod.PUT, produces = "application/json")
	public @ResponseBody
	Map editCostInterval(@RequestBody CostintervalClientObject costInterval) {
		costIntervalService.editCostInterval(costInterval);
		dailyCostService.editDailyCosts(costInterval);
		//fehlt: Kostenanpassung der Campaignangaben an die Kostenintervalle (bzgl. fixen und totalen Kosten)
		return null;
	}
	
	@RequestMapping(value = "/costIntervals/{costintervalId}", method = RequestMethod.DELETE, produces = "application/json")
	public @ResponseBody
	Map deleteCostInterval(@PathVariable Long costintervalId, @RequestParam Long campaignId) {
		costIntervalService.deleteCostInterval(costintervalId);
		dailyCostService.deleteDailyCosts(costintervalId);
		campaignService.updateCampaignAfterDeletingCostInterval(costintervalId);
		return null;
	}
	
	  @InitBinder
	    private void initBinder(WebDataBinder binder) {
		  binder.addValidators(costIntervalStartAfterEnd);
		  binder.addValidators(fixCostsOrNumberOfVariableMustBeGiven);
		  binder.addValidators(costIntervalInBetweenCampaignInterval);
		  binder.addValidators(overlappingCostIntervals);
	  }
	
}
